#include "commons.h"
#include "tasks.h"
unsigned int hit = 0, miss = 0, falseAlarm = 0, correctRejection = 0, od = 0, in = 0, correctRatio = 0, discrimRate_oneback = 0, lr = 0, lm = 0, lw = 0, lc = 0;
unsigned char text[] = {0x48, 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x47, 0x6f, 0x6f, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65};
unsigned char text1[] = {0x20, 0x20, 0x61, 0x74, 0x20, 0x4e, 0x65, 0x77, 0x20, 0x41, 0x67, 0x65, 0x20, 0x20, 0x20, 0x20};
unsigned char text7[] = {0x53, 0x5F, 0x2E, 0x5F, 0x52, 0x5F, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x5F, 0x64, 0x5F, 0x5F, 0x25};
unsigned char text7_2[] = {0x4D, 0x31, 0x5F, 0x5F, 0x52, 0x5F, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x5F, 0x64, 0x5F, 0x5F, 0x25};
unsigned char text7_3[] = {0x4E, 0x4D, 0x31, 0x5F, 0x52, 0x5F, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x5F, 0x64, 0x5F, 0x5F, 0x25};
unsigned char text7_4[] = {0x4D, 0x32, 0x5F, 0x5F, 0x52, 0x5F, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x5F, 0x64, 0x5F, 0x5F, 0x25};
unsigned char text7_5[] = {0x52, 0x61, 0x6E, 0x5F, 0x52, 0x5F, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x5F, 0x64, 0x5F, 0x5F, 0x25};
unsigned char text8[] = {0x53, 0x5F, 0x5F, 0x5F, 0x57, 0x5F, 0x5F, 0x5F, 0x43, 0x5F, 0x5F, 0x5F, 0x74, 0x5F, 0x5F, 0x5F};
unsigned char text8_2[] = {0x4E, 0x5F, 0x5F, 0x5F, 0x57, 0x5F, 0x5F, 0x5F, 0x43, 0x5F, 0x5F, 0x5F, 0x74, 0x5F, 0x5F, 0x5F};
unsigned char text9[] = {0x4C, 0x52, 0x5F, 0x5F, 0x4C, 0x4D, 0x5F, 0x5F, 0x4C, 0x57, 0x5F, 0x5F, 0x4C, 0x43, 0x5F, 0x5F};
unsigned char text10[] = {0x52, 0x5F, 0x5F, 0x4D, 0x5F, 0x5F, 0x57, 0x5F, 0x5F, 0x43, 0x5F, 0x5F, 0x74, 0x5F, 0x5F, 0x5F};
struct flagStruct Flags;
int currentMiss = 0;
unsigned int oneback_r = 0, oneback_w = 0, oneback_m = 0, oneback_c = 0;
unsigned int ITI = 5;
float LO = 0.5;
unsigned int LW = 1;
unsigned int trial = 0;
unsigned int flaga = 0;
unsigned int flagb = 0;
unsigned int ra = 0, rb = 0, rc = 0, rd = 0, re = 0;
unsigned int ma = 0, mb = 0, mc = 0, md = 0, me = 0;
unsigned int wa = 0, wb = 0, wc = 0, wd = 0, we = 0;
unsigned int ca = 0, cb = 0, cc = 0, cd = 0, ce = 0;
unsigned int ITIa = 0, ITIb = 0, ITIc = 0, ITId = 0, ITIe = 0;
unsigned int Delay = 0, Delaya = 0, Delayb = 0, Delayc = 0, Delayd = 0, Delaye = 0;
unsigned int totalTrials = 0;
unsigned int cra = 0, crb = 0, crc = 0, crd = 0, cre = 0;
unsigned int Flagsaaa = 0, Flagsaab = 0, Flagsaac = 0, Flagsaad = 0, Flagsaae = 0;
unsigned int Flagsbba = 0, Flagsbbb = 0, Flagsbbc = 0, Flagsbbd = 0, Flagsbbe = 0;
unsigned int Flagsaba = 0, Flagsabb = 0, Flagsabc = 0, Flagsabd = 0, Flagsabe = 0;
unsigned int Flagsbaa = 0, Flagsbab = 0, Flagsbac = 0, Flagsbad = 0, Flagsbae = 0;
int ValDisVer1;
int ValDisVer2;
int afterlightt = 500;

void AB_BA_Go(int interOdorDelay, int ITI, float waterlen, float delayBeforeReward) {
    localSendOnce(SpS1S1, 1);
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));
    timerCounterI = 0;
    while (timerCounterI < 2 * 1000) {
    }
    while (1) {
        int temptrial = 0;
        while (temptrial < 21) {
            int ValRan = rand() % 2;
            switch (ValRan) {
                case 0:
                    ODNMS_Go(odor_A, 0.5, interOdorDelay, odor_B, 0.5, waterlen, ITI, 0, delayBeforeReward);
                    temptrial++;
                    ODNMS_Go(odor_B, 0.5, interOdorDelay, odor_A, 0.5, waterlen, ITI, 0, delayBeforeReward);
                    temptrial++;
                    break;
                case 1:
                    ODNMS_Go(odor_B, 0.5, interOdorDelay, odor_A, 0.5, waterlen, ITI, 0, delayBeforeReward);
                    temptrial++;
                    ODNMS_Go(odor_A, 0.5, interOdorDelay, odor_B, 0.5, waterlen, ITI, 0, delayBeforeReward);
                    temptrial++;
                    break;
            }
        };
    };
}

void AB_BA_Go_shortDelay(float thisdelay, int ITI, float waterlen, float delay2) {
    localSendOnce(SpS1S1, 1);
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));
    timerCounterI = 0;
    while (timerCounterI < 2 * 1000) {
    }
    while (1) {
        int temptrial = 0;
        while (temptrial < 21) {
            int ValRan = rand() % 2;
            switch (ValRan) {
                case 0:
                    ODNMS_Go_shortDelay(odor_A, 0.5, thisdelay, odor_B, 0.5, waterlen, ITI, 0, delay2);
                    temptrial++;
                    ODNMS_Go_shortDelay(odor_B, 0.5, thisdelay, odor_A, 0.5, waterlen, ITI, 0, delay2);
                    temptrial++;
                    break;
                case 1:
                    ODNMS_Go_shortDelay(odor_B, 0.5, thisdelay, odor_A, 0.5, waterlen, ITI, 0, delay2);
                    temptrial++;
                    ODNMS_Go_shortDelay(odor_A, 0.5, thisdelay, odor_B, 0.5, waterlen, ITI, 0, delay2);
                    temptrial++;
                    break;
            }
        };
    };
}

void AA_BB_Go(int thisdelay, int ITI, float waterlen, float delay2) {
    localSendOnce(SpS1S1, 1);
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));
    timerCounterI = 0;
    while (timerCounterI < 2 * 1000) {
    }
    while (1) {
        int temptrial = 0;
        while (temptrial < 21) {
            int ValRan = rand() % 2;
            switch (ValRan) {
                case 0:
                    ODNMS_Go(odor_A, 0.5, thisdelay, odor_A, 0.5, waterlen, 5, 0, delay2);
                    temptrial++;
                    ODNMS_Go(odor_B, 0.5, thisdelay, odor_B, 0.5, waterlen, 5, 0, delay2);
                    temptrial++;
                    break;
                case 1:
                    ODNMS_Go(odor_B, 0.5, thisdelay, odor_B, 0.5, waterlen, 5, 0, delay2);
                    temptrial++;
                    ODNMS_Go(odor_A, 0.5, thisdelay, odor_A, 0.5, waterlen, 5, 0, delay2);
                    temptrial++;
                    break;
            }
        };
    };
}

void AB_BA_Go_AA_BB_Nogo_Ran(int thisdelay, int thistrialnum) {
    srand((unsigned) time(NULL));
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));
    Dispversion(3, 0);
    timerCounterI = 0;
    while (timerCounterI < 2 * 1000) {
    }
    while (1) {
        int temptrial = 0;
        while (temptrial < thistrialnum) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Sti2oDNMS_Go(odor_A, 0.5, thisdelay, odor_B, 0.5, 2, 5, 0);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Sti2oDNMS_Go(odor_B, 0.5, thisdelay, odor_A, 0.5, 2, 5, 0);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo(odor_A, 0.5, thisdelay, odor_A, 0.5, 2, 5, 0);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo(odor_B, 0.5, thisdelay, odor_B, 0.5, 2, 5, 0);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
    };
}

void auto_AB_BA_Go_AA_BB_Nogo_5delays_Ran_lesion(int D1, int D2, int D3, int D4, int D5, int ITI1, int ITI2, int ITI3, int ITI4, int ITI5) {
    Delaya = D1, Delayb = D2, Delayc = D3, Delayd = D4, Delaye = D5;
    ITIa = ITI1, ITIb = ITI2, ITIc = ITI3, ITId = ITI4, ITIe = ITI5;
    localSendOnce(SpS1S5, 1);
    localSendOnce(SpStepN, 5);
    DelayNmSecNew(2 * 1000);
    int sessionNum = 0;
    int temptrial = 0;
    int trial = 0;
    while (sessionNum < 20) {
        home_clr();
        sessionNum++;
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SpSess, 1);
        Flagsaaa = 0, Flagsaab = 2, Flagsaac = 0, Flagsaad = 2, Flagsaae = 2;
        Flagsbba = 0, Flagsbbb = 2, Flagsbbc = 0, Flagsbbd = 2, Flagsbbe = 2;
        Flagsaba = 0, Flagsabb = 2, Flagsabc = 0, Flagsabd = 2, Flagsabe = 2;
        Flagsbaa = 0, Flagsbab = 2, Flagsbac = 0, Flagsbad = 2, Flagsbae = 2;
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        ra = 0;
        rb = 0;
        rc = 0;
        rd = 0;
        re = 0;
        ma = 0;
        mb = 0;
        mc = 0;
        md = 0;
        me = 0;
        wa = 0;
        wb = 0;
        wc = 0;
        wd = 0;
        we = 0;
        ca = 0;
        cb = 0;
        cc = 0;
        cd = 0;
        ce = 0;
        while (temptrial < 8) {
            int ValRan = rand() % 20;
            switch (ValRan) {
                case 0:
                    if (Flagsaaa == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaya;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 1;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIa;
                        Delay = Delaya;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaaa = 1;
                        Stimulate3();
                        Flagsaaa = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 1:
                    if (Flagsaab == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayb;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 1;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIb;
                        Delay = Delayb;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaab = 1;
                        Stimulate3();
                        Flagsaab = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 2:
                    if (Flagsaac == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayc;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 1;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIc;
                        Delay = Delayc;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaac = 1;
                        Stimulate3();
                        Flagsaac = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 3:
                    if (Flagsaad == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayd;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 1;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITId;
                        Delay = Delayd;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaad = 1;
                        Stimulate3();
                        Flagsaad = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 4:
                    if (Flagsaae == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaye;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 1;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIe;
                        Delay = Delaye;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaae = 1;
                        Stimulate3();
                        Flagsaae = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 5:
                    if (Flagsbba == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaya;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 2;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIa;
                        Delay = Delaya;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbba = 1;
                        Stimulate4();
                        Flagsbba = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 6:
                    if (Flagsbbb == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayb;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 2;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIb;
                        Delay = Delayb;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbbb = 1;
                        Stimulate4();
                        Flagsbbb = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 7:
                    if (Flagsbbc == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayc;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 2;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIc;
                        Delay = Delayc;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbbc = 1;
                        Stimulate4();
                        Flagsbbc = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 8:
                    if (Flagsbbd == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayd;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 2;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITId;
                        Delay = Delayd;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbbd = 1;
                        Stimulate4();
                        Flagsbbd = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 9:
                    if (Flagsbbe == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaye;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 2;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIe;
                        Delay = Delaye;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbbe = 1;
                        Stimulate4();
                        Flagsbbe = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 10:
                    if (Flagsaba == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaya;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 3;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIa;
                        Delay = Delaya;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsaba = 1;
                        Stimulate1();
                        Flagsaba = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 11:
                    if (Flagsabb == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayb;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 3;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIb;
                        Delay = Delayb;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsabb = 1;
                        Stimulate1();
                        Flagsabb = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 12:
                    if (Flagsabc == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayc;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 3;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIc;
                        Delay = Delayc;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsabc = 1;
                        Stimulate1();
                        Flagsabc = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 13:
                    if (Flagsabd == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayd;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 3;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITId;
                        Delay = Delayd;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsabd = 1;
                        Stimulate1();
                        Flagsabd = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 14:
                    if (Flagsabe == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaye;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 3;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIe;
                        Delay = Delaye;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsabe = 1;
                        Stimulate1();
                        Flagsabe = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 15:
                    if (Flagsbaa == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaya;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 4;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIa;
                        Delay = Delaya;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbaa = 1;
                        Stimulate2();
                        Flagsbaa = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 16:
                    if (Flagsbab == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayb;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 4;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIb;
                        Delay = Delayb;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbab = 1;
                        Stimulate2();
                        Flagsbab = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 17:
                    if (Flagsbac == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayc;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 4;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIc;
                        Delay = Delayc;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbac = 1;
                        Stimulate2();
                        Flagsbac = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 18:
                    if (Flagsbad == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delayd;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 4;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITId;
                        Delay = Delayd;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbad = 1;
                        Stimulate2();
                        Flagsbad = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
                case 19:
                    if (Flagsbae == 2) {
                        break;
                    } else {
                        ValDisVer1 = Delaye;
                        counts_dispose(ValDisVer1);
                        LCD_set_xy(11, 2);
                        lcd_data(ValDisVer1 + 0x30);
                        ValDisVer2 = 4;
                        counts_dispose(ValDisVer2);
                        LCD_set_xy(13, 2);
                        lcd_data(ValDisVer2 + 0x30);
                        ITI = ITIe;
                        Delay = Delaye;
                        localSendOnce(SpOdorDelay, Delay);
                        Flagsbae = 1;
                        Stimulate2();
                        Flagsbae = 2;
                        trial++;
                        temptrial++;
                        break;
                    }
            }
        }
        counts_dispose(ca);
        LCD_set_xy(2, 1);
        lcd_data(ones);
        counts_dispose(cb);
        LCD_set_xy(7, 1);
        lcd_data(ones);
        counts_dispose(cc);
        LCD_set_xy(12, 1);
        lcd_data(ones);
        counts_dispose(cd);
        LCD_set_xy(2, 2);
        lcd_data(ones);
        counts_dispose(ce);
        LCD_set_xy(7, 2);
        lcd_data(ones);
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void test_water(void) {
    home_clr();
    while (1) {
        Valve_ON(water_sweet, period * 2 * 1);
    }
}

void test_odorA(void) {
    home_clr();
    while (1) {
        Valve_ON(odor_A, period * 2 * 1);
        DelayNmSecNew(2000);
        Valve_OFF(odor_A);
        DelayNmSecNew(2000);
    };
}

void test_odorB(void) {
    home_clr();
    while (1) {
        Valve_ON(odor_B, period * 2 * 1);
        DelayNmSecNew(2000);
        Valve_OFF(odor_B);
        DelayNmSecNew(2000);
    };
}

void test_odorC(void) {
    home_clr();
    while (1) {
        Valve_ON(5, period * 2 * 1);
        DelayNmSecNew(2000);
        Valve_OFF(5);
        DelayNmSecNew(2000);
    };
}

void test_odorD(void) {
    home_clr();
    while (1) {
        Valve_ON(6, period * 2 * 1);
        DelayNmSecNew(2000);
        Valve_OFF(6);
        DelayNmSecNew(2000);
    };
}

void Stimulate1(void) {
    int intTrialIntvl;
    intTrialIntvl = ITI;
    Valve_ON(odor_A, period * 2 * 1.0);
    localSendOnce(SpOdor_A, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_A);
    localSendOnce(SpOdor_A, 0);
    timerCounterI = 0;
    while (timerCounterI < (Delay * 1000)) {
    }
    Valve_ON(odor_B, period * 2 * 1.0);
    localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_B);
    localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < 0.5 * 1000) {
    }
    NewDigt_out(4, 0.5);
    float LenWater;
    LenWater = LW;
    {
    }
    timerCounterI = 0;
    while (timerCounterI < LenWater * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        if (Flagsaba == 1) {
            ra++;
            localSendOnce(SpHit, 1);
            counts_dispose(ra);
            LCD_set_xy(1, 1);
            lcd_data(ones);
        } else if (Flagsabb == 1) {
            rb++;
            localSendOnce(SpHit, 1);
            counts_dispose(rb);
            LCD_set_xy(6, 1);
            lcd_data(ones);
        } else if (Flagsabc == 1) {
            rc++;
            localSendOnce(SpHit, 1);
            counts_dispose(rc);
            LCD_set_xy(11, 1);
            lcd_data(ones);
        } else if (Flagsabd == 1) {
            rd++;
            localSendOnce(SpHit, 1);
            counts_dispose(rd);
            LCD_set_xy(1, 2);
            lcd_data(ones);
        } else if (Flagsabe == 1) {
            re++;
            localSendOnce(SpHit, 1);
            counts_dispose(re);
            LCD_set_xy(6, 2);
            lcd_data(ones);
        }
    } else {
        if (Flagsaba == 1) {
            ma++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsabb == 1) {
            mb++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsabc == 1) {
            mc++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsabd == 1) {
            md++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsabe == 1) {
            me++;
            localSendOnce(SpMiss, 1);
        }
    }
    int totalr = ra + rb + rc + rd + re;
    int totalw = wa + wb + wc + wd + we;
    int totalc = ca + cb + cc + cd + ce;
    int totalm = ma + mb + mc + md + me;
    int totalTrials = totalr + totalw + totalc + totalm;
    counts_dispose(totalTrials);
    LCD_set_xy(15, 2);
    lcd_data(tens);
    lcd_data(ones);
    if (Flagsaba == 1) {
        cra = 100 * (ra + ca) / (ra + ma + ca + wa);
        counts_dispose(cra);
        LCD_set_xy(3, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsabb == 1) {
        crb = 100 * (rb + cb) / (rb + mb + cb + wb);
        counts_dispose(crb);
        LCD_set_xy(8, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsabc == 1) {
        crc = 100 * (rc + cc) / (rc + mc + cc + wc);
        counts_dispose(crc);
        LCD_set_xy(13, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsabd == 1) {
        crd = 100 * (rd + cd) / (rd + md + cd + wd);
        counts_dispose(crd);
        LCD_set_xy(3, 2);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsabe == 1) {
        cre = 100 * (re + ce) / (re + me + ce + we);
        counts_dispose(cre);
        LCD_set_xy(8, 2);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI * 1000) {
    }
}

void Stimulate2(void) {
    int intTrialIntvl;
    intTrialIntvl = ITI;
    Valve_ON(odor_B, period * 2 * 1.0);
    localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_B);
    localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < (Delay * 1000)) {
    }
    Valve_ON(odor_A, period * 2 * 1.0);
    localSendOnce(SpOdor_A, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_A);
    localSendOnce(SpOdor_A, 0);
    timerCounterI = 0;
    while (timerCounterI < 0.5 * 1000) {
    }
    NewDigt_out(4, 0.5);
    float LenWater;
    LenWater = LW;
    {
    }
    timerCounterI = 0;
    while (timerCounterI < LenWater * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        if (Flagsbaa == 1) {
            ra++;
            localSendOnce(SpHit, 1);
            counts_dispose(ra);
            LCD_set_xy(1, 1);
            lcd_data(ones);
        } else if (Flagsbab == 1) {
            rb++;
            localSendOnce(SpHit, 1);
            counts_dispose(rb);
            LCD_set_xy(6, 1);
            lcd_data(ones);
        } else if (Flagsbac == 1) {
            rc++;
            localSendOnce(SpHit, 1);
            counts_dispose(rc);
            LCD_set_xy(11, 1);
            lcd_data(ones);
        } else if (Flagsbad == 1) {
            rd++;
            localSendOnce(SpHit, 1);
            counts_dispose(rd);
            LCD_set_xy(1, 2);
            lcd_data(ones);
        } else if (Flagsbae == 1) {
            re++;
            localSendOnce(SpHit, 1);
            counts_dispose(re);
            LCD_set_xy(6, 2);
            lcd_data(ones);
        }
    } else {
        if (Flagsbaa == 1) {
            ma++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsbab == 1) {
            mb++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsbac == 1) {
            mc++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsbad == 1) {
            md++;
            localSendOnce(SpMiss, 1);
        } else if (Flagsbae == 1) {
            me++;
            localSendOnce(SpMiss, 1);
        }
    }
    int totalr = ra + rb + rc + rd + re;
    int totalw = wa + wb + wc + wd + we;
    int totalc = ca + cb + cc + cd + ce;
    int totalm = ma + mb + mc + md + me;
    int totalTrials = totalr + totalw + totalc + totalm;
    counts_dispose(totalTrials);
    LCD_set_xy(15, 2);
    lcd_data(tens);
    lcd_data(ones);
    if (Flagsbaa == 1) {
        cra = 100 * (ra + ca) / (ra + ma + ca + wa);
        counts_dispose(cra);
        LCD_set_xy(3, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbab == 1) {
        crb = 100 * (rb + cb) / (rb + mb + cb + wb);
        counts_dispose(crb);
        LCD_set_xy(8, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbac == 1) {
        crc = 100 * (rc + cc) / (rc + mc + cc + wc);
        counts_dispose(crc);
        LCD_set_xy(13, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbad == 1) {
        crd = 100 * (rd + cd) / (rd + md + cd + wd);
        counts_dispose(crd);
        LCD_set_xy(3, 2);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbae == 1) {
        cre = 100 * (re + ce) / (re + me + ce + we);
        counts_dispose(cre);
        LCD_set_xy(8, 2);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI * 1000) {
    }
}

void Stimulate3(void) {
    int intTrialIntvl;
    intTrialIntvl = ITI;
    Valve_ON(odor_A, period * 2 * 1.0);
    localSendOnce(SpOdor_A, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_A);
    localSendOnce(SpOdor_A, 0);
    timerCounterI = 0;
    while (timerCounterI < (Delay * 1000)) {
    }
    Valve_ON(odor_A, period * 2 * 1.0);
    localSendOnce(SpOdor_A, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_A);
    localSendOnce(SpOdor_A, 0);
    timerCounterI = 0;
    while (timerCounterI < 0.5 * 1000) {
    }
    NewDigt_out(4, 0.5);
    float LenWater;
    LenWater = LW;
    {
    }
    timerCounterI = 0;
    while (timerCounterI < LenWater * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        Flags.lick = 0;
        if (Flagsaaa == 1) {
            wa++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsaab == 1) {
            wb++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsaac == 1) {
            wc++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsaad == 1) {
            wd++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsaae == 1) {
            we++;
            localSendOnce(SpFalseAlarm, 1);
        }
    } else {
        if (Flagsaaa == 1) {
            ca++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(ca);
            LCD_set_xy(2, 1);
            lcd_data(ones);
        } else if (Flagsaab == 1) {
            cb++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cb);
            LCD_set_xy(7, 1);
            lcd_data(ones);
        } else if (Flagsaac == 1) {
            cc++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cc);
            LCD_set_xy(12, 1);
            lcd_data(ones);
        } else if (Flagsaad == 1) {
            cd++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cd);
            LCD_set_xy(2, 2);
            lcd_data(ones);
        } else if (Flagsaae == 1) {
            ce++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(ce);
            LCD_set_xy(7, 2);
            lcd_data(ones);
        }
    }
    int totalr = ra + rb + rc + rd + re;
    int totalw = wa + wb + wc + wd + we;
    int totalc = ca + cb + cc + cd + ce;
    int totalm = ma + mb + mc + md + me;
    int totalTrials = totalr + totalw + totalc + totalm;
    counts_dispose(totalTrials);
    LCD_set_xy(15, 2);
    lcd_data(tens);
    lcd_data(ones);
    if (Flagsaaa == 1) {
        cra = 100 * (ra + ca) / (ra + ma + ca + wa);
        counts_dispose(cra);
        LCD_set_xy(3, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsaab == 1) {
        crb = 100 * (rb + cb) / (rb + mb + cb + wb);
        counts_dispose(crb);
        LCD_set_xy(8, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsaac == 1) {
        crc = 100 * (rc + cc) / (rc + mc + cc + wc);
        counts_dispose(crc);
        LCD_set_xy(13, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsaad == 1) {
        crd = 100 * (rd + cd) / (rd + md + cd + wd);
        counts_dispose(crd);
        LCD_set_xy(3, 2);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsaae == 1) {
        cre = 100 * (re + ce) / (re + me + ce + we);
        counts_dispose(cre);
        LCD_set_xy(8, 2);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI * 1000) {
    }
}

void Stimulate4(void) {
    int intTrialIntvl;
    intTrialIntvl = ITI;
    Valve_ON(odor_B, period * 2 * 1.0);
    localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_B);
    localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < (Delay * 1000)) {
    }
    Valve_ON(odor_B, period * 2 * 1.0);
    localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < LO * 1000) {
    }
    Valve_OFF(odor_B);
    localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < 0.5 * 1000) {
    }
    NewDigt_out(4, 0.5);
    float LenWater;
    LenWater = LW;
    {
    }
    timerCounterI = 0;
    while (timerCounterI < LenWater * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        Flags.lick = 0;
        if (Flagsbba == 1) {
            wa++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsbbb == 1) {
            wb++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsbbc == 1) {
            wc++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsbbd == 1) {
            wd++;
            localSendOnce(SpFalseAlarm, 1);
        } else if (Flagsbbe == 1) {
            we++;
            localSendOnce(SpFalseAlarm, 1);
        }
    } else {
        if (Flagsbba == 1) {
            ca++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(ca);
            LCD_set_xy(2, 1);
            lcd_data(ones);
        } else if (Flagsbbb == 1) {
            cb++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cb);
            LCD_set_xy(7, 1);
            lcd_data(ones);
        } else if (Flagsbbc == 1) {
            cc++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cc);
            LCD_set_xy(12, 1);
            lcd_data(ones);
        } else if (Flagsbbd == 1) {
            cd++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(cd);
            LCD_set_xy(2, 2);
            lcd_data(ones);
        } else if (Flagsbbe == 1) {
            ce++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(ce);
            LCD_set_xy(7, 2);
            lcd_data(ones);
        }
    }
    int totalr = ra + rb + rc + rd + re;
    int totalw = wa + wb + wc + wd + we;
    int totalc = ca + cb + cc + cd + ce;
    int totalm = ma + mb + mc + md + me;
    int totalTrials = totalr + totalw + totalc + totalm;
    counts_dispose(totalTrials);
    LCD_set_xy(15, 2);
    lcd_data(tens);
    lcd_data(ones);
    if (Flagsbba == 1) {
        cra = 100 * (ra + ca) / (ra + ma + ca + wa);
        counts_dispose(cra);
        LCD_set_xy(3, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbbb == 1) {
        crb = 100 * (rb + cb) / (rb + mb + cb + wb);
        counts_dispose(crb);
        LCD_set_xy(8, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbbc == 1) {
        crc = 100 * (rc + cc) / (rc + mc + cc + wc);
        counts_dispose(crc);
        LCD_set_xy(13, 1);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbbd == 1) {
        crd = 100 * (rd + cd) / (rd + md + cd + wd);
        counts_dispose(crd);
        LCD_set_xy(3, 2);
        lcd_data(tens);
        lcd_data(ones);
    } else if (Flagsbbe == 1) {
        cre = 100 * (re + ce) / (re + me + ce + we);
        counts_dispose(cre);
        LCD_set_xy(8, 2);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI * 1000) {
    }
}

void Sti2oDNMS_Go(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, int WaterLIn, int IntervalIn, int prolong) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
    }
    localSendOnce(SpITI, 0);
}

void Sti2oDNMS_Nogo(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, int WaterLIn, int IntervalIn, int prolong) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        Flags.lick = 0;
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    int interval = IntervalIn;
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {
    }
    localSendOnce(SpITI, 0);
}

void Dispversion(int firstN, int secondN) {
    int ValDisVer1 = firstN;
    int ValDisVer2 = secondN;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30);
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30);
}

void NewBurstStimOut(int FreqStim, int DuraStim, int totalStimTime) {
    int Interval = (1000 / FreqStim) - DuraStim;
    int ind;
    int NumSpikeBurst = (totalStimTime / 1000) * FreqStim;
    for (ind = 1; ind <= NumSpikeBurst; ind++) {
        Out5 = 1;
        timerCounterI = 0;
        while (timerCounterI < DuraStim) {
        }
        Out5 = 0;
        timerCounterI = 0;
        while (timerCounterI < Interval) {
        }
    }
}

void test_lick(void) {
    Valve_ON(water_sweet, period * 2 * 1);
    while (1) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
        }
    }
}

void FourOdor_DNMS(int O1, int O2, int O3, int O4, int thisdelay, int interti, int trialNumperSess) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    int temptrial = 0;
    int sessionNum = 0;
    while (1) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        int flag00 = 0, flag11 = 0, flag22 = 0, flag33 = 0;
        int flag01 = 0, flag10 = 0, flag02 = 0, flag20 = 0, flag03 = 0, flag30 = 0;
        int flag12 = 0, flag21 = 0, flag13 = 0, flag31 = 0;
        int flag23 = 0, flag32 = 0;
        int flagO = 0;
        while (temptrial < trialNumperSess) {
            srand((unsigned) time(NULL));
            while ((temptrial < trialNumperSess) && flagO == 0) {
                int O1Ran = rand() % 4;
                int O2Ran = rand() % 4;
                if (O1Ran == 0) {
                    if (O2Ran == 0 && flag00 != 3) {
                        O1 = odor_A;
                        O2 = odor_A;
                        flag00++;
                        flagO = 1;
                    } else if (O2Ran == 1 && flag01 != 1) {
                        O1 = odor_A;
                        O2 = odor_B;
                        flag01++;
                        flagO = 1;
                    } else if (O2Ran == 2 && flag02 != 1) {
                        O1 = odor_A;
                        O2 = odor_C;
                        flag02++;
                        flagO = 1;
                    } else if (O2Ran == 3 && flag03 != 1) {
                        O1 = odor_A;
                        O2 = odor_D;
                        flag03++;
                        flagO = 1;
                    }
                } else if (O1Ran == 1) {
                    if (O2Ran == 0 && flag10 != 1) {
                        O1 = odor_B;
                        O2 = odor_A;
                        flag10++;
                        flagO = 1;
                    } else if (O2Ran == 1 && flag11 != 3) {
                        O1 = odor_B;
                        O2 = odor_B;
                        flag11++;
                        flagO = 1;
                    } else if (O2Ran == 2 && flag12 != 1) {
                        O1 = odor_B;
                        O2 = odor_C;
                        flag12++;
                        flagO = 1;
                    } else if (O2Ran == 3 && flag13 != 1) {
                        O1 = odor_B;
                        O2 = odor_D;
                        flag13++;
                        flagO = 1;
                    }
                } else if (O1Ran == 2) {
                    if (O2Ran == 0 && flag20 != 1) {
                        O1 = odor_C;
                        O2 = odor_A;
                        flag20++;
                        flagO = 1;
                    } else if (O2Ran == 1 && flag21 != 1) {
                        O1 = odor_C;
                        O2 = odor_B;
                        flag21++;
                        flagO = 1;
                    } else if (O2Ran == 2 && flag22 != 3) {
                        O1 = odor_C;
                        O2 = odor_C;
                        flag22++;
                        flagO = 1;
                    } else if (O2Ran == 3 && flag22 != 1) {
                        O1 = odor_C;
                        O2 = odor_D;
                        flag23++;
                        flagO = 1;
                    }
                } else if (O1Ran == 3) {
                    if (O2Ran == 0 && flag30 != 1) {
                        O1 = odor_D;
                        O2 = odor_A;
                        flag30++;
                        flagO = 1;
                    } else if (O2Ran == 1 && flag31 != 1) {
                        O1 = odor_D;
                        O2 = odor_B;
                        flag31++;
                        flagO = 1;
                    } else if (O2Ran == 2 && flag32 != 1) {
                        O1 = odor_D;
                        O2 = odor_C;
                        flag32++;
                        flagO = 1;
                    } else if (O2Ran == 3 && flag33 != 3) {
                        O1 = odor_D;
                        O2 = odor_D;
                        flag33++;
                        flagO = 1;
                    }
                }
            }
            DNMS_trial_LED(O1, 0.5, thisdelay, O2, 0.5, 0.5, interti, 0, 0);
            temptrial++;
            flagO = 0;
            counts_dispose(temptrial);
            LCD_set_xy(2, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void DNMS_trial_LED(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < 0.5 * 1000) {
    }
    NewDigt_out(4, 0.5);
    if (FirstOdorIn != SecondOdorIn) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);
        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            hit++;
            localSendOnce(SpHit, 1);
            counts_dispose(hit);
            LCD_set_xy(6, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            miss++;
            localSendOnce(SpMiss, 1);
            counts_dispose(miss);
            LCD_set_xy(10, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    } else {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
            }
        }
        if (Flags.lick) {
            timerCounterI = 0;
            while (timerCounterI < prolong * 1000) {
            }
            Flags.lick = 0;
            falseAlarm++;
            currentMiss = 0;
            localSendOnce(SpFalseAlarm, 1);
            counts_dispose(falseAlarm);
            LCD_set_xy(6, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            correctRejection++;
            Flags.lick = 0;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(correctRejection);
            LCD_set_xy(10, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
    }
    localSendOnce(SpITI, 0);
}

void test_BNC(void) {
    NewDigt_out(1, 1);
    NewDigt_out(2, 1);
    NewDigt_out(3, 1);
    NewDigt_out(4, 1);
    NewDigt_out(5, 1);
    NewDigt_out(6, 1);
}

void auto_AB_BA_Go_AA_BB_Nogo_Ran_indicator_onebacktest(int thisdelay, int interti, int trialNumperSess) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while (currentMiss <= 3) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        oneback_r = 0;
        oneback_m = 0;
        oneback_w = 0;
        oneback_c = 0;
        while (temptrial < (trialNumperSess) && currentMiss <= 50) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Sti2oDNMS_Go_indicator_onebacktest(odor_A, 0.5, thisdelay, odor_B, 0.5, 0.5, interti, 0, 1);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Sti2oDNMS_Go_indicator_onebacktest(odor_B, 0.5, thisdelay, odor_A, 0.5, 0.5, interti, 0, 1);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_indicator_onebacktest(odor_A, 0.5, thisdelay, odor_A, 0.5, 0.5, interti, 0, 1);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_indicator_onebacktest(odor_B, 0.5, thisdelay, odor_B, 0.5, 0.5, interti, 0, 1);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void Sti2oDNMS_Go_indicator_onebacktest(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) {
        localSendOnce(SpOdor_A, 1);
        flaga++;
    } else if (FirstOdorIn == odor_B) {
        localSendOnce(SpOdor_B, 1);
        flagb++;
    }
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        Flags.lick = 0;
        if (flaga == 2) {
            oneback_w++;
        } else if (flagb == 2) {
            oneback_w++;
        } else if (flaga == 1 && flagb == 1) {
            oneback_r++;
        }
    } else {
        if (flaga == 2) {
            oneback_c++;
        } else if (flagb == 2) {
            oneback_c++;
        } else if (flaga == 1 && flagb == 1) {
            oneback_m++;
        }
    }
    flaga = 0;
    flagb = 0;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) {
        localSendOnce(SpOdor_A, 1);
        flaga++;
    } else if (SecondOdorIn == odor_B) {
        localSendOnce(SpOdor_B, 1);
        flagb++;
    }
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    if (oneback_r + oneback_c > 0) {
        discrimRate_oneback = 100 * (oneback_r + oneback_c) / (oneback_r + oneback_m + oneback_c + oneback_w);
        counts_dispose(discrimRate_oneback);
        LCD_set_xy(1, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        discrimRate_oneback = 0;
        counts_dispose(discrimRate_oneback);
        LCD_set_xy(1, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
    }
    localSendOnce(SpITI, 0);
}

void Sti2oDNMS_Nogo_indicator_onebacktest(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) {
        localSendOnce(SpOdor_A, 1);
        flaga++;
    } else if (FirstOdorIn == odor_B) {
        localSendOnce(SpOdor_B, 1);
        flagb++;
    }
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        Flags.lick = 0;
        if (flaga == 2) {
            oneback_w++;
        } else if (flagb == 2) {
            oneback_w++;
        } else if (flaga == 1 && flagb == 1) {
            oneback_r++;
        }
    } else {
        if (flaga == 2) {
            oneback_c++;
        } else if (flagb == 2) {
            oneback_c++;
        } else if (flaga == 1 && flagb == 1) {
            oneback_m++;
        }
    }
    flaga = 0;
    flagb = 0;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) {
        localSendOnce(SpOdor_A, 1);
        flaga++;
    } else if (SecondOdorIn == odor_B) {
        localSendOnce(SpOdor_B, 1);
        flagb++;
    }
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        Flags.lick = 0;
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    if (oneback_r + oneback_c > 0) {
        discrimRate_oneback = 100 * (oneback_r + oneback_c) / (oneback_r + oneback_m + oneback_c + oneback_w);
        counts_dispose(discrimRate_oneback);
        LCD_set_xy(1, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        discrimRate_oneback = 0;
        counts_dispose(discrimRate_oneback);
        LCD_set_xy(1, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int interval = IntervalIn;
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {
    }
    localSendOnce(SpITI, 0);
};

void ODNMS_Go(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < SecOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    localSendOnce(SpITI, 0);
}

void ODNMS_Go_shortDelay(int FirstOdorIn, float FirOdorLen, float OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < SecOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    localSendOnce(SpITI, 0);
}

void ODNMS_Nogo(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < SecOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        Flags.lick = 0;
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    int interval = IntervalIn;
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    localSendOnce(SpITI, 0);
}

void auto_AB_BA_Go_AA_BB_Nogo_Ran_new(int thisdelay, int interti, int trialNumperSess, float waterlen, int delay2, int missstop, int sessstop) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while ((currentMiss <= missstop) && (sessionNum <= sessstop)) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < (trialNumperSess) && currentMiss <= missstop) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS_Go(odor_A, 0.5, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS_Go(odor_B, 0.5, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_A, 0.5, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_B, 0.5, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void auto_AA_BB_Go_AB_BA_Nogo_Ran_new(int thisdelay, int interti, int trialNumperSess, float waterlen, int delay2, int missstop, int sessstop) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while ((currentMiss <= missstop) && (sessionNum <= sessstop)) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < (trialNumperSess) && currentMiss <= missstop) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS_Go(odor_A, 0.5, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS_Go(odor_B, 0.5, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_A, 0.5, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_B, 0.5, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void AB_BA_Go_AA_BB_Nogo_Ran_blue_delay(int thisdelay, int ITI, int ontrial, int intertrial, int offtrial, int Freq, int PulseOn) {
    localSendOnce(SpStepN, 0);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while (1) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < ontrial) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_delay(sessionNum, odor_A, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_delay(sessionNum, odor_B, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_delay(sessionNum, odor_A, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_delay(sessionNum, odor_B, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void AB_BA_Go_AA_BB_Nogo_Ran_odor(int thisdelay, int ITI, int ontrial, int intertrial, int offtrial, int Freq, int PulseOn) {
    localSendOnce(SpStepN, 0);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while (1) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < ontrial) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_odor(sessionNum, odor_A, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_odor(sessionNum, odor_B, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_odor(sessionNum, odor_A, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_odor(sessionNum, odor_B, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void AB_BA_Go_AA_BB_Nogo_Ran_ITI(int thisdelay, int ITI, int ontrial, int intertrial, int offtrial, int Freq, int PulseOn) {
    localSendOnce(SpStepN, 0);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while (1) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < ontrial) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_ITI(sessionNum, odor_A, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Sti2oDNMS_Go_opto_ITI(sessionNum, odor_B, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_ITI(sessionNum, odor_A, 0.5, thisdelay, odor_A, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            Sti2oDNMS_Nogo_opto_ITI(sessionNum, odor_B, 0.5, thisdelay, odor_B, 0.5, 0.5, ITI, 0, 1, Freq, PulseOn, ontrial, intertrial, offtrial);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void Sti2oDNMS_Go_opto_delay(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < OdorDelayIn * 1000) {
        }
    } else {
        NewBurstStimOut(Freq, PulseOn, OdorDelayIn * 1000);
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
    }
}

void Sti2oDNMS_Nogo_opto_delay(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    
    NewBurstStimOut(Freq, PulseOn, OdorDelayIn * 1000);
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        Flags.lick = 0;
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    int interval = IntervalIn;
    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {
    }
}

void Sti2oDNMS_Go_opto_odor(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < FirOdorLen * 1000) {
        }
    } else {
        NewBurstStimOut(Freq, PulseOn, FirOdorLen * 1000);
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < SecOdorLen * 1000) {
        }
    } else {
        NewBurstStimOut(Freq, PulseOn, SecOdorLen * 1000);
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
    }
}

void Sti2oDNMS_Nogo_opto_odor(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < FirOdorLen * 1000) {
        }
    } else {
        NewBurstStimOut(Freq, PulseOn, FirOdorLen * 1000);
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < SecOdorLen * 1000) {
        }
    } else {
        NewBurstStimOut(Freq, PulseOn, SecOdorLen * 1000);
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        Flags.lick = 0;
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    int interval = IntervalIn;
    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {
    }
}

void Sti2oDNMS_Go_opto_ITI(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < SecOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON(water_sweet, period * 2 * 1);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF(water_sweet);
    localSendOnce(SpWater_sweet, 0);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < IntervalIn * 1000) {
        }
    } else {
        timerCounterI = 0;
        while (timerCounterI < (IntervalIn - OdorDelayIn)*1000) {
        }
        NewBurstStimOut(Freq, PulseOn, OdorDelayIn * 1000);
    }
}

void Sti2oDNMS_Nogo_opto_ITI(int sessionNum, int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward, int Freq, int PulseOn, int ontrial, int intertrial, int offtrial) {
    if (sessionNum % 3 != 0) {
        localSendOnce(Splaser, 0);
    } else {
        localSendOnce(Splaser, 1);
    }
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
    }
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < SecOdorLen * 1000) {
    }
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
    }
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) {
        }
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        Flags.lick = 0;
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    if (sessionNum % 3 != 0) {
        timerCounterI = 0;
        while (timerCounterI < IntervalIn * 1000) {
        }
    } else {
        timerCounterI = 0;
        while (timerCounterI < (IntervalIn - OdorDelayIn)*1000) {
        }
        NewBurstStimOut(Freq, PulseOn, OdorDelayIn * 1000);
    }
}

void auto_AB_BA_Go_AA_BB_Nogo_Ran_new_new(int thisdelay, int interti, int trialNumperSess, float waterlen, int delay2, int missstop, int sessstop, float odorlen) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while ((currentMiss <= missstop) && (sessionNum <= sessstop)) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < (trialNumperSess) && currentMiss <= missstop) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS_Go(odor_A, odorlen, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS_Go(odor_B, odorlen, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_A, odorlen, thisdelay, odor_A, 0.5, waterlen, interti, 0, delay2);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS_Nogo(odor_B, odorlen, thisdelay, odor_B, 0.5, waterlen, interti, 0, delay2);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void auto_AB_BA_Go_AA_BB_Nogo_Ran_OdorControl(int thisdelay, int interti, int trialNumperSess, float waterlen, int delay2, int missstop, int sessstop, int OdorOne, int OdorTwo) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while ((currentMiss <= missstop) && (sessionNum <= sessstop)) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < (trialNumperSess) && currentMiss <= missstop) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS_Go(OdorOne, 0.5, thisdelay, OdorTwo, 0.5, waterlen, interti, 0, delay2);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS_Go(OdorTwo, 0.5, thisdelay, OdorOne, 0.5, waterlen, interti, 0, delay2);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS_Nogo(OdorOne, 0.5, thisdelay, OdorOne, 0.5, waterlen, interti, 0, delay2);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS_Nogo(OdorTwo, 0.5, thisdelay, OdorTwo, 0.5, waterlen, interti, 0, delay2);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void VaryingDelay(int OdorA, int OdorB, float Delay1, float Delay2, float Delay3, int Times1, int Times2, int Times3, int Sessions, float Waterlen, float Response, float Odorlen) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int tempInt;
    float tempFloat;
    int itr1;
    int itr2;
    for (itr1 = 1; itr1 <= Sessions; itr1++) {
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(itr1);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        DelayNmSecNew(2 * 1000);
        int FirstOdor[41] = {0};
        int SecondOdor[41] = {0};
        float DelayDuration[41] = {0};
        for (itr2 = 1; itr2 <= 4 * (Times1 + Times2 + Times3); itr2++) {
            if (itr2 % 4 == 1) {
                FirstOdor[itr2] = OdorA;
                SecondOdor[itr2] = OdorB;
            }
            if (itr2 % 4 == 2) {
                FirstOdor[itr2] = OdorB;
                SecondOdor[itr2] = OdorA;
            }
            if (itr2 % 4 == 3) {
                FirstOdor[itr2] = OdorA;
                SecondOdor[itr2] = OdorA;
            }
            if (itr2 % 4 == 0) {
                FirstOdor[itr2] = OdorB;
                SecondOdor[itr2] = OdorB;
            }
            if (itr2 <= 4 * Times1) {
                DelayDuration[itr2] = Delay1;
            } else if (itr2 <= 4 * (Times1 + Times2)) {
                DelayDuration[itr2] = Delay2;
            } else if (itr2 <= 4 * (Times1 + Times2 + Times3)) {
                DelayDuration[itr2] = Delay3;
            }
        }
        for (itr2 = 1; itr2 <= 4 * (Times1 + Times2 + Times3); itr2++) {
            int ind = rand() % (4 * (Times1 + Times2 + Times3) - itr2 + 1) + 1;
            localSendOnce(SpSessHit, ind);
            if (FirstOdor[ind] == SecondOdor[ind]) {
                ODNMS_Nogo(FirstOdor[ind], Odorlen, DelayDuration[ind], SecondOdor[ind], Odorlen, Waterlen, DelayDuration[ind] + 6.0, 0, Response);
            } else if (FirstOdor[ind] != SecondOdor[ind]) {
                ODNMS_Go(FirstOdor[ind], Odorlen, DelayDuration[ind], SecondOdor[ind], Odorlen, Waterlen, DelayDuration[ind] + 6.0, 0, Response);
            }
            tempInt = FirstOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            FirstOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            SecondOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempFloat = DelayDuration[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            DelayDuration[4 * (Times1 + Times2 + Times3) - itr2 + 1] = DelayDuration[ind];
            DelayDuration[ind] = tempFloat;
        }
    }
}

void VaryingOdor(int OdorA, int OdorB, float Odor1, float Odor2, float Odor3, int Times1, int Times2, int Times3, int Sessions, float Waterlen, float Response, float Delay) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int tempInt;
    float tempFloat;
    int itr1;
    int itr2;
    for (itr1 = 1; itr1 <= Sessions; itr1++) {
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(itr1);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        DelayNmSecNew(2 * 1000);
        int FirstOdor[41] = {0};
        int SecondOdor[41] = {0};
        float FirstOdorDuration[41] = {0};
        for (itr2 = 1; itr2 <= 4 * (Times1 + Times2 + Times3); itr2++) {
            if (itr2 % 4 == 1) {
                FirstOdor[itr2] = OdorA;
                SecondOdor[itr2] = OdorB;
            }
            if (itr2 % 4 == 2) {
                FirstOdor[itr2] = OdorB;
                SecondOdor[itr2] = OdorA;
            }
            if (itr2 % 4 == 3) {
                FirstOdor[itr2] = OdorA;
                SecondOdor[itr2] = OdorA;
            }
            if (itr2 % 4 == 0) {
                FirstOdor[itr2] = OdorB;
                SecondOdor[itr2] = OdorB;
            }
            if (itr2 <= 4 * Times1) {
                FirstOdorDuration[itr2] = Odor1;
            } else if (itr2 <= 4 * (Times1 + Times2)) {
                FirstOdorDuration[itr2] = Odor2;
            } else if (itr2 <= 4 * (Times1 + Times2 + Times3)) {
                FirstOdorDuration[itr2] = Odor3;
            }
        }
        for (itr2 = 1; itr2 <= 4 * (Times1 + Times2 + Times3); itr2++) {
            int ind = rand() % (4 * (Times1 + Times2 + Times3) - itr2 + 1) + 1;
            if (FirstOdor[ind] == SecondOdor[ind]) {
                ODNMS_Nogo(FirstOdor[ind], FirstOdorDuration[ind], Delay, SecondOdor[ind], 1.0, Waterlen, Delay + 6.0, 0, Response);
            } else if (FirstOdor[ind] != SecondOdor[ind]) {
                ODNMS_Go(FirstOdor[ind], FirstOdorDuration[ind], Delay, SecondOdor[ind], 1.0, Waterlen, Delay + 6.0, 0, Response);
            }
            tempInt = FirstOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            FirstOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            SecondOdor[4 * (Times1 + Times2 + Times3) - itr2 + 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempFloat = FirstOdorDuration[4 * (Times1 + Times2 + Times3) - itr2 + 1];
            FirstOdorDuration[4 * (Times1 + Times2 + Times3) - itr2 + 1] = FirstOdorDuration[ind];
            FirstOdorDuration[ind] = tempFloat;
        }
    }
}

void shaping_main2(int thisdelay, int interti, int trialNumperSess, float delay2, int missstop, int sessstop) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);
    DelayNmSecNew(2 * 1000);
    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while ((currentMiss < missstop) && (sessionNum < sessstop)) {
        sessionNum++;
        localSendOnce(SpStepN, 3);
        localSendOnce(SpSessN, sessionNum);
        localSendOnce(SptrialNperSess, trialNumperSess);
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        timerCounterI = 0;
        while (timerCounterI < 3000) {
        }
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < (trialNumperSess) && currentMiss < missstop) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4 && currentMiss < missstop) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            shaping_go2(odor_A, 0.5, thisdelay, odor_B, 0.5, interti, delay2);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            shaping_go2(odor_B, 0.5, thisdelay, odor_A, 0.5, interti, delay2);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            shaping_go2(odor_A, 0.5, thisdelay, odor_A, 0.5, interti, delay2);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            shaping_go2(odor_B, 0.5, thisdelay, odor_B, 0.5, interti, delay2);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            };
        };
        localSendOnce(SpSessHit, hit);
        localSendOnce(SpSessMiss, miss);
        localSendOnce(SpSessFA, falseAlarm);
        localSendOnce(SpSessCR, correctRejection);
        localSendOnce(SpSessCorrRate, correctRatio);
        localSendOnce(SpSess, 0);
    };
    localSendOnce(SpTrain, 0);
}

void shaping_go2(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, int IntervalIn, float delay_before_reward) {
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
        }
    }
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
        }
    }
    if (FirstOdorIn == odor_A) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < 1 * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);
        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            hit++;
            localSendOnce(SpHit, 1);
            counts_dispose(hit);
            LCD_set_xy(6, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            miss++;
            localSendOnce(SpMiss, 1);
            counts_dispose(miss);
            LCD_set_xy(10, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + falseAlarm) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    if (FirstOdorIn == odor_B) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < 1 * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);
        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            falseAlarm++;
            localSendOnce(SpFalseAlarm, 1);
            counts_dispose(falseAlarm);
            LCD_set_xy(6, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            correctRejection++;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(correctRejection);
            LCD_set_xy(10, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + falseAlarm) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < 5 * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
        }
    }
    localSendOnce(SpITI, 0);
}

void NewBurstStimOut2(int FreqStim, int DuraStim, int totalStimTime) {
    int Interval = (1000 / FreqStim) - DuraStim;
    int ind;
    int NumSpikeBurst = (totalStimTime / 1000) * FreqStim;
    for (ind = 1; ind <= NumSpikeBurst; ind++) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < DuraStim) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        Out3 = 0;
        timerCounterI = 0;
        while (timerCounterI < Interval) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
}

void rule_switch_MNM(int thisdelay, int interti, int trialNumperSess, float WaterLen, int thresh, int randomtrial_n, int delay_before_reward) {
    int flag1 = 0;
    int flag2 = 0;
    int flag3 = 0;
    int flag4 = 0;
    int session_n1 = 0;
    int session_n2 = 0;
    int session_n3 = 0;
    int temptrial = 0;
    int trial = 0;
    while (currentMiss < 6) {
        session_n1++;
        if (session_n1 >= 5) {
            if (session_n1 % 4 == 1) {
                flag1 = 0;
            } else if (session_n1 % 4 == 2) {
                flag2 = 0;
            } else if (session_n1 % 4 == 3) {
                flag3 = 0;
            } else if (session_n1 % 4 == 0) {
                flag4 = 0;
            }
        }
        home_clr();
        puts_lcd((unsigned char*) &text7_2[0], sizeof (text7_2));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n1);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        int flagall = 0;
        flagall = flag1 + flag2 + flag3 + flag4;
        counts_dispose(flag1);
        LCD_set_xy(4, 1);
        lcd_data(ones);
        counts_dispose(flag2);
        LCD_set_xy(5, 1);
        lcd_data(ones);
        counts_dispose(flag3);
        LCD_set_xy(6, 1);
        lcd_data(ones);
        counts_dispose(flag4);
        LCD_set_xy(7, 1);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        if ((session_n1 % 4 == 1) && correctRatio >= thresh) {
            flag1++;
        } else if ((session_n1 % 4 == 2) && correctRatio >= thresh) {
            flag2++;
        } else if ((session_n1 % 4 == 3) && correctRatio >= thresh) {
            flag3++;
        } else if ((session_n1 % 4 == 0) && correctRatio >= thresh) {
            flag4++;
        }
        localSendOnce(SpSess, 0);
        if (session_n1 >= 4 && (flag1 + flag2 + flag3 + flag4) >= 3) {
            break;
        }
    }
    temptrial = 0;
    hit = 0;
    miss = 0;
    falseAlarm = 0;
    correctRejection = 0;
    home_clr();
    puts_lcd((unsigned char*) &text7_5[0], sizeof (text7_5));
    line_2();
    puts_lcd((unsigned char*) &text8_2[0], sizeof (text8_2));
    int temprandomtrial_n = 1 + rand() % randomtrial_n;
    int temp_trial_n = 0;
    counts_dispose(temprandomtrial_n);
    LCD_set_xy(2, 2);
    lcd_data(tens);
    lcd_data(ones);
    while (temp_trial_n < temprandomtrial_n) {
        int Ran = rand() % 4;
        switch (Ran) {
            case 0:
            {
                ODMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 1:
            {
                ODMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 2:
            {
                ODMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 3:
            {
                ODMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
        }
    }
    localSendOnce(SpSess, 0);
    int flagall = 0;
    flag1 = 0;
    flag2 = 0;
    flag3 = 0;
    flag4 = 0;
    while (currentMiss < 6) {
        session_n2++;
        if (session_n2 >= 5) {
            if (session_n2 % 4 == 1) {
                flag1 = 0;
            } else if (session_n2 % 4 == 2) {
                flag2 = 0;
            } else if (session_n2 % 4 == 3) {
                flag3 = 0;
            } else if (session_n2 % 4 == 0) {
                flag4 = 0;
            }
        }
        home_clr();
        puts_lcd((unsigned char*) &text7_3[0], sizeof (text7_3));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n2);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        flagall = flag1 + flag2 + flag3 + flag4;
        counts_dispose(flagall);
        LCD_set_xy(4, 1);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        if ((session_n2 % 4 == 1) && correctRatio >= thresh) {
            flag1++;
        } else if ((session_n2 % 4 == 2) && correctRatio >= thresh) {
            flag2++;
        } else if ((session_n2 % 4 == 3) && correctRatio >= thresh) {
            flag3++;
        } else if ((session_n2 % 4 == 0) && correctRatio >= thresh) {
            flag4++;
        }
        localSendOnce(SpSess, 0);
        if (session_n2 >= 4 && (flag1 + flag2 + flag3 + flag4) >= 3) {
            break;
        }
    }
    while (currentMiss < 6) {
        session_n3++;
        home_clr();
        puts_lcd((unsigned char*) &text7_4[0], sizeof (text7_4));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n3);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void rule_switch_NMN(int thisdelay, int interti, int trialNumperSess, float WaterLen, int thresh, int randomtrial_n, int delay_before_reward) {
    int flag1 = 0;
    int flag2 = 0;
    int flag3 = 0;
    int flag4 = 0;
    int session_n1 = 0;
    int session_n2 = 0;
    int session_n3 = 0;
    int temptrial = 0;
    int trial = 0;
    while (currentMiss < 6) {
        session_n1++;
        if (session_n1 >= 5) {
            if (session_n1 % 4 == 1) {
                flag1 = 0;
            } else if (session_n1 % 4 == 2) {
                flag2 = 0;
            } else if (session_n1 % 4 == 3) {
                flag3 = 0;
            } else if (session_n1 % 4 == 0) {
                flag4 = 0;
            }
        }
        home_clr();
        puts_lcd((unsigned char*) &text7_2[0], sizeof (text7_2));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n1);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        int flagall = 0;
        flagall = flag1 + flag2 + flag3 + flag4;
        counts_dispose(flag1);
        LCD_set_xy(4, 1);
        lcd_data(ones);
        counts_dispose(flag2);
        LCD_set_xy(5, 1);
        lcd_data(ones);
        counts_dispose(flag3);
        LCD_set_xy(6, 1);
        lcd_data(ones);
        counts_dispose(flag4);
        LCD_set_xy(7, 1);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        if ((session_n1 % 4 == 1) && correctRatio >= thresh) {
            flag1++;
        } else if ((session_n1 % 4 == 2) && correctRatio >= thresh) {
            flag2++;
        } else if ((session_n1 % 4 == 3) && correctRatio >= thresh) {
            flag3++;
        } else if ((session_n1 % 4 == 0) && correctRatio >= thresh) {
            flag4++;
        }
        localSendOnce(SpSess, 0);
        if (session_n1 >= 4 && (flag1 + flag2 + flag3 + flag4) >= 3) {
            break;
        }
    }
    temptrial = 0;
    hit = 0;
    miss = 0;
    falseAlarm = 0;
    correctRejection = 0;
    home_clr();
    puts_lcd((unsigned char*) &text7_5[0], sizeof (text7_5));
    line_2();
    puts_lcd((unsigned char*) &text8_2[0], sizeof (text8_2));
    int temprandomtrial_n = 1 + rand() % randomtrial_n;
    int temp_trial_n = 0;
    counts_dispose(temprandomtrial_n);
    LCD_set_xy(2, 2);
    lcd_data(tens);
    lcd_data(ones);
    while (temp_trial_n < temprandomtrial_n) {
        int Ran = rand() % 4;
        switch (Ran) {
            case 0:
            {
                ODNMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 1:
            {
                ODNMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 2:
            {
                ODNMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
            case 3:
            {
                ODNMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                temp_trial_n++;
                break;
            }
        }
    }
    localSendOnce(SpSess, 0);
    int flagall = 0;
    flag1 = 0;
    flag2 = 0;
    flag3 = 0;
    flag4 = 0;
    while (currentMiss < 6) {
        session_n2++;
        if (session_n2 >= 5) {
            if (session_n2 % 4 == 1) {
                flag1 = 0;
            } else if (session_n2 % 4 == 2) {
                flag2 = 0;
            } else if (session_n2 % 4 == 3) {
                flag3 = 0;
            } else if (session_n2 % 4 == 0) {
                flag4 = 0;
            }
        }
        home_clr();
        puts_lcd((unsigned char*) &text7_3[0], sizeof (text7_3));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n2);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        flagall = flag1 + flag2 + flag3 + flag4;
        counts_dispose(flagall);
        LCD_set_xy(4, 1);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        if ((session_n2 % 4 == 1) && correctRatio >= thresh) {
            flag1++;
        } else if ((session_n2 % 4 == 2) && correctRatio >= thresh) {
            flag2++;
        } else if ((session_n2 % 4 == 3) && correctRatio >= thresh) {
            flag3++;
        } else if ((session_n2 % 4 == 0) && correctRatio >= thresh) {
            flag4++;
        }
        localSendOnce(SpSess, 0);
        if (session_n2 >= 4 && (flag1 + flag2 + flag3 + flag4) >= 3) {
            break;
        }
    }
    while (currentMiss < 6) {
        session_n3++;
        home_clr();
        puts_lcd((unsigned char*) &text7_4[0], sizeof (text7_4));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(session_n3);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (temptrial < trialNumperSess && currentMiss < 6) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            Flags.aa = 0;
            Flags.bb = 0;
            while (trial < 4) {
                int ValRan = rand() % 4;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 2:
                        if (Flags.aa) {
                            break;
                        } else {
                            ODNMS(odor_A, 0.5, thisdelay, odor_B, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.aa = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                    case 3:
                        if (Flags.bb) {
                            break;
                        } else {
                            ODNMS(odor_B, 0.5, thisdelay, odor_A, 0.5, WaterLen, interti, 0, delay_before_reward);
                            Flags.bb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}



void ODNMS(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    if (FirstOdorIn != SecondOdorIn) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);
        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            hit++;
            localSendOnce(SpHit, 1);
            counts_dispose(hit);
            LCD_set_xy(6, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            miss++;
            localSendOnce(SpMiss, 1);
            counts_dispose(miss);
            LCD_set_xy(10, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    } else {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
                Flags.pun = 1;
            }
            if (Flags.pun == 1 && Flags.punable == 1) {
                Valve_ON(4, period * 2 * 1);
                DelayNmSecNew(200);
                Valve_OFF(4);
                Flags.punable = 0;
            }
        }
        Flags.pun = 0;
        Flags.punable = 1;
        if (Flags.lick) {
            timerCounterI = 0;
            while (timerCounterI < prolong * 1000) {
            }
            Flags.lick = 0;
            falseAlarm++;
            currentMiss = 0;
            localSendOnce(SpFalseAlarm, 1);
            counts_dispose(falseAlarm);
            LCD_set_xy(6, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            correctRejection++;
            Flags.lick = 0;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(correctRejection);
            LCD_set_xy(10, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) {
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
}

void ODMS(int FirstOdorIn, float FirOdorLen, int OdorDelayIn, int SecondOdorIn, float SecOdorLen, float WaterLIn, int IntervalIn, int prolong, int delay_before_reward) {
    Valve_ON(FirstOdorIn, period * 2 * 1.0);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(FirstOdorIn);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_ON(SecondOdorIn, period * 2 * 1.0);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        if (LickOnPin) {
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF(SecondOdorIn);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) {
        if (LickOnPin) {
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
    if (FirstOdorIn == SecondOdorIn) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);
        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            hit++;
            localSendOnce(SpHit, 1);
            counts_dispose(hit);
            LCD_set_xy(6, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            miss++;
            localSendOnce(SpMiss, 1);
            counts_dispose(miss);
            LCD_set_xy(10, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    } else {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
                Flags.pun = 1;
            }
            if (Flags.pun == 1 && Flags.punable == 1) {
                Valve_ON(4, period * 2 * 1);
                DelayNmSecNew(200);
                Valve_OFF(4);
                Flags.punable = 0;
            }
        }
        Flags.pun = 0;
        Flags.punable = 1;
        if (Flags.lick) {
            timerCounterI = 0;
            while (timerCounterI < prolong * 1000) {
            }
            Flags.lick = 0;
            falseAlarm++;
            currentMiss = 0;
            localSendOnce(SpFalseAlarm, 1);
            counts_dispose(falseAlarm);
            LCD_set_xy(6, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            correctRejection++;
            Flags.lick = 0;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(correctRejection);
            LCD_set_xy(10, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) {
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON(4, period * 2 * 1);
            DelayNmSecNew(200);
            Valve_OFF(4);
            Flags.punable = 0;
        }
    }
    Flags.pun = 0;
    Flags.punable = 1;
}



void go_nogo(int trialInSesion, int go_odor, int nogo_odor, float odorLen, float delay, float waterlen,                                                                   \
             int ITI, int odorlaseryes, int delaylaseryes, int waterlaseryes, int ITI1, int ITI2) {
    localSendOnce(SpStepN, 0);
    DelayNmSecNew(2 * 1000);
    int currentTrial = 0;
    int trial = 0;
    int sessionNum = 0;
    while (1) {
        sessionNum++;
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        currentTrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        while (currentTrial < trialInSesion) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;
            while (trial < 2) {
                int ValRan = rand() % 2;
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            go(go_odor, nogo_odor, odorLen, delay, waterlen, ITI, odorlaseryes, delaylaseryes, waterlaseryes, ITI1, ITI2);
                            Flags.ab = 1;
                            trial++;
                            currentTrial++;
                            break;
                        }
                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            nogo(go_odor, nogo_odor, odorLen, delay, waterlen, ITI, odorlaseryes, delaylaseryes, waterlaseryes, ITI1, ITI2);
                            Flags.ba = 1;
                            trial++;
                            currentTrial++;
                            break;
                        }
                }
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);
}

void go(int go_odor, int nogo_odor, float odorLen, float delay, float waterlen,                                                                   \
             int ITI, int odorlaseryes, int delaylaseryes, int waterlaseryes, int ITI1, int ITI2) {
    Valve_ON(go_odor, period * 2 * 1.0);
    if (go_odor == odor_A) localSendOnce(SpOdor_A, 1);
    else if (go_odor == odor_B) localSendOnce(SpOdor_B, 1);
    if (odorlaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < odorLen * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        Out3 = 0;
    } else {
        timerCounterI = 0;
        while (timerCounterI < odorLen * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Valve_OFF(go_odor);
    if (delaylaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (0.25 * delay * 1000 < timerCounterI && timerCounterI < 0.75 * delay * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        Out3 = 0;
    } else {
        timerCounterI = 0;
        while (timerCounterI < delay * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Flags.lick = 0;
    if (waterlaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < waterlen * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
    } else {
        timerCounterI = 0;
        while (timerCounterI < waterlen * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2 * 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
    }
    Valve_OFF(water_sweet);
    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / totalTrials;
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI1 * 1000) {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }
    if (ITI2 != 0) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < ITI2 * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Out3 = 0;
    timerCounterI = 0;
    while (timerCounterI < (ITI - ITI1 - ITI2) * 1000) {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }
}

void nogo(int go_odor, int nogo_odor, float odorLen, float delay, float waterlen,                                                                   \
          int ITI, int odorlaseryes, int delaylaseryes, int waterlaseryes, int ITI1, int ITI2) {
    Valve_ON(nogo_odor, period * 2 * 1.0);
    if (nogo_odor == odor_A) localSendOnce(SpOdor_A, 1);
    else if (nogo_odor == odor_B) localSendOnce(SpOdor_B, 1);
    if (odorlaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < odorLen * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        Out3 = 0;
    } else {
        timerCounterI = 0;
        while (timerCounterI < odorLen * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Valve_OFF(nogo_odor);
    if (delaylaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (0.25 * delay * 1000 < timerCounterI && timerCounterI < 0.75 * delay * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        Out3 = 0;
    } else {
        timerCounterI = 0;
        while (timerCounterI < delay * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Flags.lick = 0;
    if (waterlaseryes == 1) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < waterlen * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
    } else {
        timerCounterI = 0;
        while (timerCounterI < waterlen * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
    }
    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < 0 * 1000) {
        }
        Flags.lick = 0;
        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }
    timerCounterI = 0;
    while (timerCounterI < ITI1 * 1000) {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }
    if (ITI2 != 0) {
        Out3 = 1;
        timerCounterI = 0;
        while (timerCounterI < ITI2 * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Out3 = 0;
    timerCounterI = 0;
    while (timerCounterI < (ITI - ITI1 - ITI2) * 1000) {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }
}
